package com.atxiao.demo.service;

import com.atxiao.demo.bean.Account;
import com.atxiao.demo.bean.Result;
import com.atxiao.demo.bean.User;
import com.atxiao.demo.mapper.TransferMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static java.lang.Double.parseDouble;

/**
 * author:黄大旺
 */
@Service
public class TransferServicelmpl implements TransferService {
    @Resource
    private TransferMapper mapper;

    @Override
    @Transactional
    /**
     * time：2021/1:45
     * 修改人：李潇潇
     * 问题描述：
     * 修改了转账逻辑
     * 问题一：余额要账户表中进行查询
     * 问题二：两个卡号混淆了
     * 问题三：A和B转账完了以后，在更新的时候对应的卡号应该填写正确
     * 问题四：插入和更新不需要返回值
     * 问题五：当来到了转账页面以后，session中的用户不为空，不需要重复判断
     */
    public Result transfer(HttpServletRequest req) {

        Result result = new Result();
        Result flag = null;
        String account = (req.getParameter("account") == null ? "" : req.getParameter("account").trim());
        String reduceAccount = (req.getParameter("reduceAccount") == null ? "" : req.getParameter("reduceAccount").trim());
        //转账的金额
        String money= (req.getParameter("money") == null ? "" : req.getParameter("money").trim());
        //根据卡号查询出来用户
        User info = mapper.select(account);
        if(info != null){
            //要转的金额
            double addMoney = parseDouble(money);
            //查询转款账户余额
//            double  remainder = mapper.selectRem(reduceAccount);
            double  remainder = mapper.selectRem(account);
            if(addMoney > remainder){
            return result.errorResult("账户余额不足");
            }

//            //标记一------------------
//            double balance = parseDouble(info.getBalance())+parseDouble(money);

            //收款人数据
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String time = df.format(new Date());// new Date()为获取当前系统时间

            int transfer_result = 0 ;
            int type = 3;
            String place = "手机银行";
         // String putName = info.getName();
//            String putAccount = info.getCardNum();
            int status = 1;
            //转账流水记录
            double reduceBalance = remainder-parseDouble(money);    //转完账以后账户剩下的钱
            int reduce = mapper.reduceMoney(reduceAccount,reduceBalance);   //更新用户的余额
            //在流水表中进行跟新一条用户转账并且余额减少的操作
            int reduceCount = mapper.reduceIncome(account,addMoney,time,status,reduceBalance,type,place,transfer_result,reduceAccount);
//            int reduceCount = mapper.reduceIncome(reduceAccount,addMoney,time,status,reduceBalance,type,place,transfer_result,putAccount);


//            =============================================================



            //收款流水记录
             status = 0;  //收钱

             //查询对方账余额
            final double opBalance = mapper.selectRem(reduceAccount);
            //对方账户现在的余额
            double newBalance=opBalance+addMoney;

            //对方账户更新
            int addAccount = mapper.addMoney(reduceAccount,newBalance);
            //流水表中更新一条对方账户余额增加的操作
            int count = mapper.insertIncome(account,addMoney,time,status,newBalance,type,place,transfer_result,reduceAccount);



            flag = result.success();
        }else{
            flag = result.errorResult();
        }
        return flag;
    }
}
